package com.wongsir.step4.tiny_ioc.factory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.wongsir.step4.tiny_ioc.BeanDefinition;


/**
 * step3：抽象工厂实现类
 * @author wongsir
 * @date 2018年9月13日
 */
public abstract class AbstractBeanFactory implements BeanFactory{

	private Map<String,BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>();
	
	@Override
	public Object getBean(String name) {
		return beanDefinitionMap.get(name).getBean();
	}

	@Override
	public void registerBeanDefinition(String name, BeanDefinition beanDefinition) throws Exception {
		Object bean = doCreateBean(beanDefinition);
		beanDefinition.setBean(bean);
		beanDefinitionMap.put(name, beanDefinition);
	}

	/**
	 * 初始化bean
	 * @param beanDefinition
	 * @return
	 * @throws Exception
	 */
	protected abstract Object doCreateBean(BeanDefinition beanDefinition) throws Exception;

}
